package com.ruoyi.web.controller.ksdH5;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.utils.Constans;
import com.ruoyi.common.utils.http.HttpUtils;

/**
 * APP 智能收款
 * 
 * @author ruoyi
 * @date 2019-06-27
 */
@Controller		 
@RequestMapping("/h5/smart")
public class KsdH5SmartController 
{
	
	
	/**
	  * 跳转收款明细
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("list")
	 public String toSmartListPage(HttpServletRequest request,HttpServletResponse response,ModelMap map)  {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
		//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
					return "h5/realName/realName";
				}
			 HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="";
			String  result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/i/getZNSKDetail", params,head);
			map.addAttribute("result", result);
			return"h5/smart/list";
	 } 
	
	/**
	  * 跳转智能收款页面
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @GetMapping("smart")
	 public String toSmartPage(HttpServletRequest request,HttpServletResponse response,ModelMap map) throws UnsupportedEncodingException {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
		//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
					return "h5/realName/realName";
				}
	/*		HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="";
			String  result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/i/cardList", params,head);
			map.addAttribute("cardList", result);*/
			return"h5/smart/smart";
	 } 
	 
	 /**
	  * 跳转智能收款页面
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @GetMapping("cardList")	 
	 @ResponseBody
	 public  Map<String, Object> cardList(HttpServletRequest request,HttpServletResponse response)  {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
		 Map<String, Object> map = new HashMap<>();
		//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
				return map;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				 return map;
				}
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="";
			String  result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/i/cardList", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	 
	 /**
	  * 验证交易密码
	  * 
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @PostMapping("checkTransPW")
	 @ResponseBody
	 public Map<String, Object> checkTransPW(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
				return map;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				 return map;
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			 String transPW = request.getParameter("transPW");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="transPW="+Ksdh5Util.md5Str(transPW);
			params.replaceAll(" ", "");
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/k/checkTransPW", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	 
	 
	 
	 
	 
	 /**
	  * 提交智能收款
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @PostMapping("submitSmart")
	 @ResponseBody
	 public Map<String, Object> submitSmart(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			 String from_bankcard = request.getParameter("from_bankcard");
			 String to_bankcard = request.getParameter("to_bankcard");
			 String collection_money = request.getParameter("collection_money");
			 String collection_addr = request.getParameter("collection_addr");
			 if (collection_addr.indexOf("-")!=-1) {
					String[] split = collection_addr.split("-");
					collection_addr = split[1];
				}
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
						
			String params="from_bankcard="+from_bankcard+"&"
						   +"to_bankcard="+to_bankcard+"&"
						   +"collection_money="+collection_money+"&"
						   +"collection_addr="+URLEncoder.encode(collection_addr,"UTF-8");
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/i/intelligentCollection", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	  
}